package cn.crc.pjblog.servlet;

import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.crc.pjblog.data.AppServletContext;
import cn.crc.pjblog.data.Constant;


public class SysListener 
extends HttpServlet 
implements ServletContextListener 
{
	
	private static final long serialVersionUID = -3843870941979017909L;

	private static final Log _log = LogFactory.getLog(SysListener.class);

	Timer aTimer = new Timer(true);

	//Notification that the web module is ready to process requests
	public void contextInitialized(ServletContextEvent sce) {

		String rootpath = sce.getServletContext().getRealPath("/");
		if (rootpath != null) {
			rootpath = rootpath.replaceAll("\\\\", "/");
		} else {
			rootpath = "/";
		}
		if (!rootpath.endsWith("/")) {
			rootpath = rootpath + "/";
		}
		Constant.ROOTPATH = rootpath;
		_log.info("Application Run Path:" + rootpath);
		
		//Store servletContext 
		AppServletContext.setServletContext(sce.getServletContext());

	}

	//Notification that the servlet context is about to be shut down
	public void contextDestroyed(ServletContextEvent sce) {
		aTimer.cancel();
	}
}
